﻿Imports Devart.Data.PostgreSql
Public Class Conexion
    Public Sub con()

        TextBox7.Text = String.Format("User Id={0};Password={1};Host={2};Database={3};Persist Security Info=True;Schema={4}", usuario.Text, contraseña.Text, host.Text, base.Text, schema.Text)
        cadenaConexion.Text = ConexionSGS.ObtenerCadenaDeConexionConContrasenaOculta(TextBox7.Text)

    End Sub
    Public Sub defa()
        host.Text = My.Resources.ConexionSGS.host
        puerto.Text = My.Resources.ConexionSGS.puerto
        schema.Text = My.Resources.ConexionSGS.schema
        usuario.Text = My.Resources.ConexionSGS.usuario
        base.Text = My.Resources.ConexionSGS.db
    End Sub
    Private Sub Conexion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fic As String = My.Resources.ConexionSGS.archivoConfiguaracion
        Dim texto As String = ""
        Try
            Dim sr As New System.IO.StreamReader(fic)
            texto = sr.ReadToEnd()
            sr.Close()
            Console.WriteLine(texto)
        Catch ex As Exception
        End Try

        Dim conexion5 As New Devart.Data.PostgreSql.PgSqlConnection(texto)
        Try
            'Si encontro la conexion en el archivo
            conexion5.Open()
            My.Settings.CambiaCnxDB = texto

            login.Show()


        Catch ex As Exception
            con()
            defa()
        End Try

    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click

        defa()
    End Sub

    Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton3.Click
        Me.Close()
    End Sub

    Private Sub SimpleButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton4.Click
        Dim conexion As New Devart.Data.PostgreSql.PgSqlConnection(TextBox7.Text)
        Try
            conexion.Open()
            MsgBox("Prueba de conexión satisfactoria", MsgBoxStyle.Information, "Prueba de conexión")
        Catch ex As Exception
            MsgBox("No se puedo establecer la conexión con la base de datos, para mayor información consulte con el personal técnico", MsgBoxStyle.Critical, "Error con la conexión")

        End Try
    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click




        Dim conexion2 As New Devart.Data.PostgreSql.PgSqlConnection(TextBox7.Text)
        Try
            'Para comprobar si existe una conexion
            conexion2.Open()
            '************************************************

            'Elimina el archivo
            Try
                My.Computer.FileSystem.DeleteFile(My.Resources.ConexionSGS.archivoConfiguaracion)
            Catch ex As Exception
            End Try
            '************************************************

            'Codigo para escribir en el archivo
            My.Settings.CambiaCnxDB = TextBox7.Text

            Dim fic As String = My.Resources.ConexionSGS.archivoConfiguaracion
            Dim diract As String = ""
            diract = System.IO.Directory.GetCurrentDirectory()

            Dim direccion As String = ""

            direccion = String.Format("{0}\{1}", diract, fic)

            Dim host1 As String = host.Text
            Dim usuario1 As String = usuario.Text
            Dim contrasena1 As String = contraseña.Text
            Dim base1 As String = base.Text
            Dim schema1 As String = schema.Text
            Dim sw As New System.IO.StreamWriter(direccion, True)

            sw.WriteLine(String.Format("User Id={0};Password={1};Host={2};Database={3};Persist Security Info=True;Schema={4}", usuario1, contrasena1, host1, base1, schema1))
            sw.Close()
            '************************************************

            Me.Hide()

            login.Show()





        Catch ex As Exception
            MsgBox("No se pudo establecer la conexión con la base de datos, para mayor información consulte con el personal técnico", MsgBoxStyle.Critical, "Error con la conexión")
        End Try


    End Sub

    Private Sub host_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles host.TextChanged
        con()
    End Sub

    Private Sub puerto_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles puerto.TextChanged
        con()
    End Sub

    Private Sub usuario_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles usuario.TextChanged

    End Sub

    Private Sub base_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles base.TextChanged
        con()
    End Sub

    Private Sub schema_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles schema.TextChanged
        con()
    End Sub

    Private Sub contraseña_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles contraseña.TextChanged
        con()
    End Sub
End Class